package localserver;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import common.AdministratorService;
import common.PlayerService;

public class LocalServer {

	public static void main(String[] args) {
		try {

			String serverGeolocation = args[0];

			int RMIPort = Integer.parseInt(common.Tools.getproperty(
					"server.properties", "RMIPort" + serverGeolocation));
			Registry registry = LocateRegistry.createRegistry(RMIPort);
			// Player part
			PlayerService playerService = new PlayerServiceImpl(
					serverGeolocation);
			String playerRMISce = common.Tools.getproperty("server.properties",
					"player" + serverGeolocation);
			registry.rebind(playerRMISce, playerService);

			System.out.println("Player service started at: "
					+ serverGeolocation + " as: " + playerRMISce);

			common.Tools.writeLog("server", serverGeolocation,
					"Player service started at: " + serverGeolocation
							+ " server as: " + playerRMISce, true);

			// Administrator part

			AdministratorService administratorService = new AdministratorServiceImpl(
					serverGeolocation);
			String administratorRMISce = common.Tools.getproperty(
					"server.properties", "administrator" + serverGeolocation);
			registry.rebind(administratorRMISce, administratorService);

			System.out.println("Administrator RMI service started at: "
					+ serverGeolocation + " as: " + administratorRMISce);
			common.Tools.writeLog("server", serverGeolocation,
					"Administrator RMI service started at: "
							+ serverGeolocation + " server as: "
							+ administratorRMISce, true);

			common.Tools.writeLog(
					"server",
					serverGeolocation,
					"Administrator UDP service started at: "
							+ serverGeolocation
							+ " server on the port: "
							+ common.Tools.getproperty("server.properties",
									"UDPPort_" + serverGeolocation), true);
			// use a thread to run the UDP server
			UDPTools.UDPServer(Integer.parseInt(common.Tools.getproperty(
					"server.properties", "UDPPort_" + serverGeolocation)),
					serverGeolocation);

			System.out.println("Administrator UDP service started at: "
					+ serverGeolocation
					+ " on the port: "
					+ common.Tools.getproperty("server.properties", "UDPPort_"
							+ serverGeolocation));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}